package org.example.JVM.ByteCode.Type;

/**
 * @description:基本单位，长度为1字节
 * @author: farprt
 * @create: 2022-11-13 14:30
 **/
public class U1 {

    private byte value;

    public U1(byte b1) {
        this.value = b1;
    }

    public Integer toInt() {
        return value & 0xff;
    }

    public String toHexString() {
        char[] hexChar = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        StringBuilder hexStr = new StringBuilder();
        int v = value & 0xff;
        if (v == 0) {
            hexStr.insert(0, "00");
        }
        while (v > 0) {
            int c = v % 16;
            v = v >>> 4;
            hexStr.insert(0, hexChar[c]);
        }
        if (((hexStr.length() & 0x01) == 1)) {
            hexStr.insert(0, '0');
        }
        return "0x" + hexStr.toString();
    }
}